package com.hxzy.controller.api.system;

import com.hxzy.common.vo.CodeEnum;
import com.hxzy.common.vo.R;
import com.hxzy.dto.system.SysMenuAllDTO;
import com.hxzy.dto.system.SysMenuDTO;
import com.hxzy.entity.system.SysMenu;
import com.hxzy.service.system.SysMenuService;
import com.hxzy.vo.system.HxzyUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = "后台员工")
@RestController
@RequestMapping(value = "/api")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "分页查询菜单信息")
    @GetMapping(value = "sysMenu/data")
    public R searchALl(SysMenuDTO sysMenuDTO){
        return R.success(this.sysMenuService.selectMenuTreeAll(sysMenuDTO));
    }

    @ApiOperation(value = "新增数据")
    @PostMapping(value = "/sysMenu/add")
    public R addSysMenu(@RequestBody @Valid SysMenu sysMenu){
        HxzyUser hxzyUser= (HxzyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        sysMenu.setCreateBy(hxzyUser.getSysUser().getName());
        sysMenu.setCreateTime(new Date());

        boolean save=this.sysMenuService.save(sysMenu);
        return save? R.success():R.failed();
    }

    @ApiOperation(value = "删除数据")
    @PostMapping(value = "/sysMenu/delete")
    public R deleteSysMenu(@RequestBody Long[] ids){
        List<Long> list=new ArrayList<>(Arrays.asList(ids));
        System.out.println(list);
        this.sysMenuService.removeBatchByIds(list);
        return R.success();
    }

    @ApiOperation(value = "修改数据")
    @PostMapping(value = "/sysMenu/update")
    public R updateSysMenu(@RequestBody SysMenu sysMenu){

        HxzyUser hxzyUser= (HxzyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysMenu db = this.sysMenuService.getById(sysMenu.getId());
        if (db!=null){
            db.setMenuName(sysMenu.getMenuName());
            db.setParentId(sysMenu.getParentId());
            db.setOrderNum(sysMenu.getOrderNum());
            db.setPath(sysMenu.getPath());
            db.setComponent(sysMenu.getComponent());
            db.setQuery(sysMenu.getQuery());
            db.setIsFrame(sysMenu.getIsFrame());
            db.setIsCache(sysMenu.getIsCache());
            db.setMenuType(sysMenu.getMenuType());
            db.setVisible(sysMenu.getVisible());
            db.setStatus(sysMenu.getStatus());
            db.setPerms(sysMenu.getPerms());
            db.setIcon(sysMenu.getIcon());
            db.setRemark(sysMenu.getRemark());
            db.setUpdateBy(hxzyUser.getSysUser().getName());
            db.setUpdateTime(new Date());

            boolean save = this.sysMenuService.updateById(db);
            return save? R.success():R.failed();
        }else {
            return R.failed(CodeEnum.EDIT_DATA_NOT_FOUND);
        }
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/sysMenu/{id}")
    public R findById(@PathVariable(value = "id") Long id){
        SysMenu sysMenu=this.sysMenuService.findById(id);
        if (sysMenu==null){
            return R.failed(CodeEnum.EDIT_DATA_NOT_FOUND);
        }
        return R.success(sysMenu);
    }

    @ApiOperation(value = "得到一部分的菜单树列结构")
    @GetMapping(value = "/sysmenu/tree")
    public R getRoleMenuTree(){
        return R.success(this.sysMenuService.buildTreeMenu());
    }

    @ApiOperation(value = "完整版菜单树列结构")
    @GetMapping(value = "/sysMenu/treeAll")
    public R getRoleMenuTreeAll(SysMenuAllDTO sysMenuAllDTO){
        return R.success(this.sysMenuService.buildTreeMenuAll(sysMenuAllDTO));
    }

    @ApiOperation(value = "所有查询菜单信息")
    @GetMapping(value = "sysMenu/dataAll")
    public R dataALl(SysMenuAllDTO sysMenuAllDTO){
        return R.success(this.sysMenuService.selectMenuAll(sysMenuAllDTO));
    }

}
